<template>
  <div id="student">
    <!-- 顶部导航 -->
    <el-row class="padding-50">
      <el-col :span="24">
        <ul class="list">
          <li class="logo">
            <i class="iconfont icon-kaoshi"></i>
            <span>DORO-Learning-Online</span>
          </li>
          <li class="logo">章节学习</li>
          <li><router-link :to="`/student/learn_document/${courseId}`">课程资料</router-link></li>
          <li><router-link :to="`/student/learn_info/${courseId}`">课程信息</router-link></li>
          <li><router-link to="/student/message">我的消息</router-link></li>
          <li><router-link to="/student">返回首页</router-link></li>
          <li class="right" @mouseenter="flag = true" @mouseleave="flag = false">
            <a href="javascript:;">
              <i class="iconfont icon-Userselect icon"></i>{{ user.userName }}
            </a>
            <div class="msg" v-if="flag">
              <p @click="manage">管理中心</p>
              <p class="exit" @click="exit">退出</p>
            </div>
          </li>
        </ul>
      </el-col>
    </el-row>

    <!-- 主体章节内容 -->
    <div class="main">
      <div v-if="chapters.length">
        <ul class="chapter-list">
          <chapter-item
            v-for="chapter in chapters"
            :key="chapter.id"
            :chapter="chapter"
            @view-file="viewFile"
          />
        </ul>
      </div>
      <div v-else>暂无章节内容</div>
    </div>

    <router-view></router-view>
    <v-footer></v-footer>
  </div>
</template>

<script>
import myFooter from '@/components/student/myFooter'
import { mapState } from 'vuex'

export default {
  components: {
  'v-footer': myFooter,
  ChapterItem: {
    name: 'ChapterItem',
    props: ['chapter'],
    data() {
      return { expanded: false }
    },
    methods: {
      toggle() {
        this.expanded = !this.expanded
      },
      view(file) {
        this.$emit('view-file', file)
      }
    },
    template: `
      <li v-if="chapter">
        <div class="chapter-title" @click="toggle">{{ chapter.title }}</div>
        <ul v-if="expanded" class="child-list">
          <li
            v-for="file in chapter.files"
            :key="file.fakeId"
            class="file"
            @click="view(file)"
          >
            📄 {{ file.name }}
          </li>
          <ChapterItem
            v-for="sub in chapter.children"
            v-if="sub"
            :key="sub.id"
            :chapter="sub"
            @view-file="view"
          />
        </ul>
      </li>
    `
  }
},
  data() {
    return {
      flag: false,
      user: {},
      courseId: null,
      courseInfo: {},
      chapters: []
    }
  },
  created() {
    this.userInfo()
    this.courseId = this.$route.params.courseId
    //this.loadCourseInfo()
    this.loadChapters()
  },
  methods: {
    userInfo() {
      const name = this.$cookies.get('cname')
      const id = this.$cookies.get('cid')
      this.user = { userName: name, studentId: id }
    },
    exit() {
      this.$cookies.remove('cname')
      this.$cookies.remove('cid')
      this.$router.push('/')
    },
    manage() {
      this.$router.push('/student/manager')
    },
    /*loadCourseInfo() {
      this.$axios.get('/api/course/info', {
        params: { courseId: this.courseId }
      }).then(res => {
        if (res.data && res.data.code === 200) {
          this.courseInfo = res.data.data
        } else {
          this.$message.error(res.data.msg || '课程信息加载失败')
        }
      }).catch(() => {
        this.$message.error('请求失败')
      })
    },*/
    loadChapters() {
      this.$axios.get('/api/chapter/courseid', {
        params: { courseID: this.courseId }
      }).then(res => {
        if (res.data.code === 200) {
          this.chapters = this.appendFakeFiles(res.data.data)
        } else {
          this.$message.error('章节加载失败')
        }
      }).catch(() => {
        this.$message.error('请求失败')
      })
    },
    appendFakeFiles(list) {
  let idGen = 1
  const append = chapter => {
    chapter.files = [
      { name: '文件1', fakeId: idGen++, chapterId: chapter.id },
      { name: '文件2', fakeId: idGen++, chapterId: chapter.id }
    ]
    // 确保 children 是数组
    chapter.children = Array.isArray(chapter.children)
      ? chapter.children.map(append)
      : []
    return chapter
  }
  return list.map(append)
},
    viewFile(file) {
      this.$message.success(`已查看 ${file.name}，进度+1`)
      // 假装调用接口：this.$axios.post('/api/learn/progress', { fileId: file.fakeId })
    }
  },
  computed: mapState(['isPractice'])
}
</script>

<style scoped>
.right .icon {
  margin-right: 6px;
}
#student .padding-50 {
  margin: 0 auto;
  padding: 0 50px;
  box-shadow: 0 0 10px 4px rgba(1, 149, 255, 0.1);
  background-color: #fff;
}
.list a {
  text-decoration: none;
  color: #334046;
}
li {
  list-style: none;
  line-height: 60px;
}
#student .list {
  display: flex;
}
#student .list li {
  padding: 0 20px;
  cursor: pointer;
}
#student .list li:hover {
  background-color: #0195ff;
  transition: all 0.3s ease;
}
#student .list li:hover a {
  color: #fff;
}
#student .list .right {
  margin-left: auto;
  position: relative;
}
#student .list li.right:hover a {
  color: #000;
}
#student .list .logo {
  display: flex;
  font-weight: bold;
  color: #2f6c9f;
}
#student .list .logo i {
  font-size: 50px;
}
.right .msg {
  text-align: center;
  position: absolute;
  top: 60px;
  left: 0;
  display: flex;
  flex-direction: column;
  border-radius: 2px;
  border-bottom: 3px solid #0195ff;
  background-color: #fff;
}
.right .msg p {
  height: 40px;
  line-height: 40px;
  width: 105px;
}
.right .msg p:hover {
  background-color: #0195ff;
}
.chapter-title {
  font-weight: bold;
  margin: 10px 0;
  cursor: pointer;
  color: #2f6c9f;
}
.child-list {
  margin-left: 20px;
}
.file {
  cursor: pointer;
  color: #3c8dbc;
  margin-left: 15px;
}
.file:hover {
  text-decoration: underline;
}
</style>
